<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Editor</title>
    <style>
        body, html {
            padding: 0;
            margin: 0;
        }
    </style>
</head>
<body>
<div style="width:100%;height:100%;position:absolute;display:flex;flex-direction:column;">
    <div style="padding:1rem;border-bottom:1px solid #EEE;">
        这里模拟一个编辑器，跨技术栈可以很方便的使用 iframe 嵌入
    </div>
    <div style="padding:1rem;flex-grow:1;">
        <textarea id="content" style="width:100%;height:100%;border:1px solid #111;box-sizing:border-box;"></textarea>
    </div>
</div>
<script>
    (function () {
        const getData = async () => {
            return new Promise((resolve) => {
                resolve(document.querySelector('#content').value);
            });
        };
        const setData = async (data) => {
            return new Promise((resolve) => {
                document.querySelector('#content').value = data;
                resolve();
            });
        };
        window.editorInit = function (success, error) {
            console.log('editorInit', success, error);
            success();
        };
        window.editorGetContent = function (success, error) {
            console.log('editorGetContent', success, error);
            getData().then(success).catch(error);
        };
        window.editorSetContent = function (success, error, content) {
            console.log('editorSetContent', success, error, content);
            setData(content).then(success).catch(error);
        };
        window.editorGetImage = function (success, error) {
            console.log('editorGetImage', success, error);
            setTimeout(() => {
                success('');
            }, 1000);
        };
        window.editorGetSvg = function (success, error) {
            console.log('editorGetSvg', success, error);
            setTimeout(() => {
                success('');
            }, 1000);
        };
        // 两种二选一，第一种，使用 input 事件监听内容变化
        document.querySelector('#content').addEventListener('input', function () {
            console.log('editorContentChange');
            window.parent.postMessage({
                type: 'ContentChange',
                data: document.querySelector('#content').value
            }, '*');
        });
        // 两种二选一，第二种，使用定时器轮询内容变化
        let lastContentJson = null;
        setInterval(async () => {
            const content = await getData();
            if(!lastContentJson){
                lastContentJson = JSON.stringify(content);
                return;
            }
            if (lastContentJson !== JSON.stringify(content)) {
                lastContentJson = JSON.stringify(content);
                console.log('editorContentChange');
                window.parent.postMessage({
                    type: 'ContentChange',
                    data: content,
                }, '*');
            }
        }, 1000);
    })();
</script>
</body>
</html>
